﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using office = Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support.Enums;

namespace MS.InformationManagement.BLL.DocumentPrinter
{
    public class UpdateWeighUpDocumentCommand : IBaseCommand
    {

        #region Fields

        EnvíoDeCorrespondenciaDTO[] _deliveries;
        string _currentUser;

        #endregion

        #region Constructors

        public UpdateWeighUpDocumentCommand(EnvíoDeCorrespondenciaDTO[] deliveries)
        {
            _deliveries = deliveries;
        }

        #endregion

        #region Implemented Command

        public void Execute()
        {
            _currentUser = SPContext.Current.Web.CurrentUser.LoginName;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        currentSite.AllowUnsafeUpdates = true;

                        foreach (var delivery in _deliveries)
                        {
                            if (delivery.IsCopy)
                                modifyCopy(delivery.LibraryName, delivery.DocumentId, currentWeb);
                            else
                               modifyDocument(delivery.LibraryName, delivery.DocumentId, currentWeb); 
                        }

                        currentWeb.AllowUnsafeUpdates = false;
                        currentSite.AllowUnsafeUpdates = false;
                    }
                }
            });     
        }

        public void Undo()
        {
        }

        #endregion
        
        #region Private Methods

        private void modifyCopy(string libraryName, int documentId, SPWeb currentWeb)
        {
            SPListItem selectedCopy = getCopy(libraryName, documentId, currentWeb);
            selectedCopy[CustomSiteColumns.FLD_COPY_PESADO] = true;
            selectedCopy.Log(string.Empty, DateTime.Now, TraceableOperation.Pesado, _currentUser, string.Empty);
            selectedCopy.Update();
        }

        private void modifyDocument(string libraryName, int documentId, SPWeb currentWeb)
        {
            office.DocumentSet selectedCopy = office.DocumentSet.GetDocumentSet(currentWeb.Lists[libraryName].GetItemById(documentId).Folder);
            selectedCopy.Folder.Item[CustomSiteColumns.FIELD_DOCUMENT_STATUS_WEIGHT] = true;
            selectedCopy.Log(string.Empty, DateTime.Now, TraceableOperation.Pesado, _currentUser, string.Empty);
            selectedCopy.Folder.Item.SystemUpdate();
            selectedCopy.Folder.Update();           
        }

        private SPListItem getCopy(string library, int parentid, SPWeb currentWeb)
        {
            return currentWeb.Lists[library].GetItemById(parentid);
        }

        #endregion


    }
}
